/**
 * Created by Administrator on 2016/11/16.
 */

var express = require('express');
var router = express.Router();
var co = require('co');
var seqModule = require('./createModules');
var seq = new seqModule();

var formidable = require('formidable');
var getIP = require('./getIP');
var fs = require('fs');
//查询

router.get('/getRoleMenu', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};
        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.menuId != undefined){
            params["menuId"]=req.query.menuId;
        }
        if(req.query.roleId != undefined){
            params["roleId"]=req.query.roleId;
        }
        paramT["where"]=params;
        seq.powerRoleMenuModule.findAll(paramT).then(function(result){
            var idArr = [];
            for(var i = 0;i<result.length;i++){
                idArr.push(result[i].menuId);
            }
            if(idArr.length > 0){
                var paramM = {};
                paramM.where = {"id":[idArr]}
                paramM.order = [["code","ASC"]];
                seq.powerMenuModule.findAll(paramM).then(function(results){
                    res.json({"success":true,"msg":"查询成功","result":results});
                 })
            }

        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

router.get('/getPowerRoleMenu', function(req, res, next) {
    co(function () {
        var params={};
        var paramT={};


        if(req.query.id != undefined){
            params["id"]=req.query.id;
        }
        if(req.query.menuId != undefined){
            params["menuId"]=req.query.menuId;
        }
        if(req.query.roleId != undefined){
            params["roleId"]=req.query.roleId;
        }
        paramT["where"]=params;
        seq.powerRoleMenuModule.findAll(paramT).then(function(result){
            res.json({"success":true,"msg":"查询成功","result":result});
        });
    }).catch(function(e) {
        res.json({'success':false,'msg':'查询失败'});
        console.log(e);
    })
});

//修改


module.exports = router;
